package com.woniuxy.usercoachassemble.controller;
//文件上传


import com.woniuxy.usercoachassemble.commons.QiNiuUtils;
import com.woniuxy.usercoachassemble.commons.RandomUtils;
import com.woniuxy.usercoachassemble.commons.UUIDUtils;
import org.example.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.transform.Source;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
@RequestMapping("file")
@RestController
public class FileupController {
    @RequestMapping("fileup")
    public Result uploadImg(MultipartFile file) throws IOException {
        //首先校验图片格式
        System.out.println(file);
        try {
            List<String> imgTypes = new ArrayList();
            //"jpg", "jpeg", "png", "bmp", "gif"
            imgTypes.add("jpg");
            imgTypes.add("jpeg");
            imgTypes.add("png");
            imgTypes.add("bmp");
            imgTypes.add("gif");

            // 获取文件名，带后缀
            String fileName = file.getOriginalFilename();
            // 获取文件的后缀格式
            String fileSuffix = fileName.substring(fileName.lastIndexOf(".")+1).toLowerCase();
            if(imgTypes.contains(fileSuffix)){
//        获取文件流发到7牛云
                String newFileName= UUIDUtils.getUUID();
                InputStream inputStream = file.getInputStream();
                QiNiuUtils.uploadQNImg(inputStream,newFileName);
                return  Result.Success(QiNiuUtils.fullPath(newFileName));
        }
            return Result.Error(110,"格式不对");

        }
        catch (Exception e){
            e.printStackTrace();
          throw  e;
        }

    }



}
